/**
 * GTActivityNotifier.m
 *
 * The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy of the License 
 * at http://www.mozilla.org/MPL/
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
 * either express or implied. See the License for the specific language governing rights and limitations 
 * under the License.
 *
 * The Original Code is gcast-toolbox4iphone.
 *
 * The Initial Developers of the Original Code is Giannandrea Castaldi.
 */
#import "GTActivityNotifier.h"
#import "UIKit+GTExtensions.h"
#import "UIWindowsApplicationDelegate.h"

static GTActivityNotifier *singleton;

@interface GTActivityNotifier ()
- (void)notifyStartActivityWithWheelInPosition:(GTActivityNotifierWheelPosition)aPosition;
- (void)notifyStartActivityWithWheelInPosition:(GTActivityNotifierWheelPosition)aPosition alpha:(CGFloat)alpha;
- (void)notifyStartActivityWithWheelInPosition:(GTActivityNotifierWheelPosition)aPosition inView:(UIView*)aView;
- (void)notifyStartActivityWithWheelInPosition:(GTActivityNotifierWheelPosition)aPosition inView:(UIView*)aView alpha:(CGFloat)alpha;
- (void)notifyStartActivityWithCustomView:(UIView*)aCustomActivityView inView:(UIView*)aView alpha:(CGFloat)alpha;
- (void)notifyMessage:(NSString*)aMessage;
@end

@implementation GTActivityNotifier

+ (void)notifyStartActivityWithWheelInPosition:(GTActivityNotifierWheelPosition)aPosition {
  if (singleton) {
    [GTActivityNotifier notifyEndActivity];
  }
  singleton = [[GTActivityNotifier alloc] init];
  [singleton notifyStartActivityWithWheelInPosition:aPosition];
}

+ (void)notifyStartActivityWithWheelInPosition:(GTActivityNotifierWheelPosition)aPosition alpha:(CGFloat)alpha {
  if (singleton) {
    [GTActivityNotifier notifyEndActivity];
  }
  singleton = [[GTActivityNotifier alloc] init];
  [singleton notifyStartActivityWithWheelInPosition:aPosition alpha:alpha];
}

+ (void)notifyStartActivityWithWheelInPosition:(GTActivityNotifierWheelPosition)aPosition inView:(UIView*)aView {
  if (singleton) {
    [GTActivityNotifier notifyEndActivity];
  }
  singleton = [[GTActivityNotifier alloc] init];
  [singleton notifyStartActivityWithWheelInPosition:aPosition inView:aView];
}

+ (void)notifyStartActivityWithWheelInPosition:(GTActivityNotifierWheelPosition)aPosition inView:(UIView*)aView alpha:(CGFloat)alpha {
  if (singleton) {
    [GTActivityNotifier notifyEndActivity];
  }
  singleton = [[GTActivityNotifier alloc] init];
  [singleton notifyStartActivityWithWheelInPosition:aPosition inView:aView alpha:alpha];
}

+ (void)notifyStartActivityWithCustomView:(UIView*)anActivityView inView:(UIView*)aView alpha:(CGFloat)alpha {
  if (singleton) {
    [GTActivityNotifier notifyEndActivity];
  }
  singleton = [[GTActivityNotifier alloc] init];
  [singleton notifyStartActivityWithCustomView:anActivityView inView:aView alpha:alpha];
}

+ (void)notifyMessage:(NSString*)aMessage {
  [singleton notifyMessage:aMessage];
}

+ (void)notifyEndActivity {
  [singleton release];
  singleton = nil;
}

- (void)dealloc {
  [activityWheel stopAnimating];
  [activityWheel release];
  [customActivityView release];
  [activityView removeFromSuperview];
  [activityView release];
  [activityMessage release];
  [super dealloc];
}

- (void)notifyStartActivityWithWheelInPosition:(GTActivityNotifierWheelPosition)aPosition {
	UIWindow *window = [(id<UIWindowsApplicationDelegate>)[[UIApplication sharedApplication] delegate] window];
  [self notifyStartActivityWithWheelInPosition:aPosition inView:(UIView*)window];
}

- (void)notifyStartActivityWithWheelInPosition:(GTActivityNotifierWheelPosition)aPosition alpha:(CGFloat)alpha {
	UIWindow *window = [(id<UIWindowsApplicationDelegate>)[[UIApplication sharedApplication] delegate] window];
  [self notifyStartActivityWithWheelInPosition:aPosition inView:(UIView*)window alpha:alpha];
}

- (void)notifyStartActivityWithWheelInPosition:(GTActivityNotifierWheelPosition)aPosition inView:(UIView*)aView {
  [self notifyStartActivityWithWheelInPosition:aPosition inView:aView alpha:0.5f];
}

- (void)notifyStartActivityWithWheelInPosition:(GTActivityNotifierWheelPosition)aPosition inView:(UIView*)aView alpha:(CGFloat)alpha {
  wheelPosition = aPosition;
  
  // Activity view
	activityView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, aView.bounds.size.width, aView.bounds.size.height)];  
	activityView.backgroundColor = [UIColor blackColor];
	activityView.alpha = alpha;
  
  // Activity wheel
	CGRect activityWheelRect;
  switch (wheelPosition) {
    case GTActivityNotifierWheelPositionCenter:
      activityWheelRect = CGRectMake(activityView.frame.size.width / 2 - 15, activityView.frame.size.height / 2 - 15, 30, 30);
      break;
    case GTActivityNotifierWheelPositionUp:
      activityWheelRect = CGRectMake(activityView.frame.size.width / 2 - 15, activityView.frame.size.height / 3 - 15, 30, 30);
      break;
    case GTActivityNotifierWheelPositionLeft:
      activityWheelRect = CGRectMake(5, activityView.frame.size.height / 2 - 12, 24, 24);      
      break;
  }
  activityWheel = [[UIActivityIndicatorView alloc] initFlexibleWithFrame:activityWheelRect
                                              activityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
	[activityView addSubview:activityWheel];
  
  // Activity message
  if (wheelPosition == GTActivityNotifierWheelPositionUp || wheelPosition == GTActivityNotifierWheelPositionLeft) {
    switch (wheelPosition) {
      case GTActivityNotifierWheelPositionUp:
        activityMessage = [[UITextView alloc] initWithFrame:CGRectMake(0.0, activityWheel.frame.origin.y + activityWheel.frame.size.height + 10, 320.0, 200.0)];
        activityMessage.font = [UIFont fontWithName:@"Helvetica-Bold" size:17.0];
        activityMessage.textAlignment = UITextAlignmentCenter;
        break;
      case GTActivityNotifierWheelPositionLeft:
        activityMessage = [[UITextView alloc] initWithFrame:CGRectMake(activityWheel.frame.origin.x + activityWheel.frame.size.width + 5, 5, 300.0, 35.0)];
        activityMessage.font = [UIFont fontWithName:@"Helvetica-Bold" size:17.0];
        activityMessage.textAlignment = UITextAlignmentLeft;
        break;
      default:
        break;
    }
    activityMessage.backgroundColor = [UIColor clearColor];
    activityMessage.textColor = [UIColor whiteColor];
    activityMessage.editable = FALSE;
    activityMessage.scrollEnabled = FALSE;
    activityMessage.text = @"";
    [activityView addSubview:activityMessage];    
  }
  
	[aView addSubview:activityView];
  [aView bringSubviewToFront:activityView];
	[activityWheel startAnimating];
}

- (void)notifyStartActivityWithCustomView:(UIView*)aCustomActivityView inView:(UIView*)aView alpha:(CGFloat)alpha {
  customActivityView = [aCustomActivityView retain];

  // Activity view
	activityView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, aView.bounds.size.width, aView.bounds.size.height)];  
	activityView.backgroundColor = [UIColor blackColor];
	activityView.alpha = alpha;
  
	[activityView addSubview:customActivityView];
  
  // Activity message
  activityMessage = [[UITextView alloc] initWithFrame:CGRectMake(0.0, aCustomActivityView.frame.origin.y + aCustomActivityView.frame.size.height + 10, 320.0, 200.0)];
  activityMessage.font = [UIFont fontWithName:@"Helvetica-Bold" size:17.0];
  activityMessage.textAlignment = UITextAlignmentCenter;
  activityMessage.backgroundColor = [UIColor clearColor];
  activityMessage.textColor = [UIColor whiteColor];
  activityMessage.editable = FALSE;
  activityMessage.scrollEnabled = FALSE;
  activityMessage.text = @"";
  [activityView addSubview:activityMessage];    
  
	[aView addSubview:activityView];
  [aView bringSubviewToFront:activityView];
}

- (void)notifyMessage:(NSString*)aMessage {
  [activityMessage performSelectorOnMainThread:@selector(setText:) withObject:aMessage waitUntilDone:NO];
}

@end
